// ignore_for_file: unused_import
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wallpaper/components/placeholder/avatar.dart';
import 'package:wallpaper/components/my_tab_header.dart';
import 'package:wallpaper/generated/l10n.dart';
import 'package:wallpaper/model/SliverTabBarDelegate.dart';
import 'package:wallpaper/pages/navbar/child/category_page.dart';
import 'package:wallpaper/pages/navbar/child/mobile_home.dart';
import 'package:wallpaper/pages/navbar/home_drawer.dart';
import 'package:wallpaper/pages/navbar/my_page.dart';
import 'package:wallpaper/pages/navbar/video_page.dart';
import 'package:wallpaper/pages/other/pixiv/pixiv_image.dart';
import 'package:wallpaper/pages/other/search/search_image.dart';
import 'package:wallpaper/themes/theme_provider.dart';
import 'package:wallpaper/tools/down_image.dart';
import 'package:wallpaper/tools/route_animation.dart';
import 'package:wallpaper/tools/update_apk.dart';
import 'dart:math' as math;

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

// 底部导航项数据模型
class BottomNavItem {
  final IconData icon;
  final String label;
  final Widget page;

  BottomNavItem({
    required this.icon,
    required this.label,
    required this.page,
  });
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  bool isScrolling = false; // 滚动状态
  // 定义底部导航项数据
  final List<BottomNavItem> navItems = [
    BottomNavItem(
      icon: Icons.whatshot_outlined,
      label: '热门',
      page: MobileHome(order: 'hot'),
    ),
    BottomNavItem(
      icon: Icons.widgets_outlined,
      label: '分类',
      page: CategoryPage(),
    ),
    BottomNavItem(
      icon: Icons.video_collection_outlined,
      label: '视频',
      page: VideoPage(),
    ),
    BottomNavItem(
      icon: Icons.storage_outlined,
      label: '库',
      page: MyPage(),
    ),
  ];

  void onScrollStart() {
    if (!isScrolling) {
      setState(() {
        isScrolling = true;
      });
    }
  }

  void onScrollEnd() {
    if (isScrolling) {
      setState(() {
        isScrolling = false;
      });
    }
  }

  Future<void> init() async {
    double size = await DownImage.getTotalCacheSize();
    if (size > 1500) {
      DownImage.clearAllCache();
    }
  }

  @override
  void initState() {
    super.initState();
    UpdateApk().updateApk(context);
    init();
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    return Scaffold(
      drawer: HomeDrawer(),
      body: buildHomeBody(theme),
    );
  }

  Widget buildHomeBody(ColorScheme theme) {
    return Stack(
      children: [
        MobileHome(
          order: 'new',
          onScrollEnd: onScrollEnd,
          onScrollStart: onScrollStart,
        ),
        Positioned(
          top: 0,
          left: 0,
          width: MediaQuery.of(context).size.width,
          child: Container(
            width: double.infinity,
            height: 80,
            padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
            color: theme.primaryContainer.withOpacity(0.3),
            child: Row(children: [
              Builder(builder: (context) {
                return IconButton(
                  onPressed: () {
                    Scaffold.of(context).openDrawer();
                  },
                  icon: Icon(Icons.menu),
                );
              }),
              Text('波奇壁纸',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  )),
              Expanded(child: SizedBox()),
              IconButton(
                onPressed: () {
                  RouteAnimation().animateRoute(context, SearchImage());
                },
                icon: Icon(Icons.search),
              ),
            ]),
          ),
        ),
        Positioned(
          bottom: 0,
          left: 0,
          width: MediaQuery.of(context).size.width,
          child: AnimatedSlide(
            offset: isScrolling ? const Offset(0, 1) : Offset.zero,
            duration: const Duration(milliseconds: 500),
            curve: Curves.easeInOut,
            child: BottomNavigationBar(
              onTap: (index) {
                RouteAnimation().animateRoute(context, navItems[index].page);
              },
              currentIndex: 0,
              type: BottomNavigationBarType.fixed,
              elevation: 1,
              backgroundColor: theme.primaryContainer.withOpacity(0.75),
              selectedItemColor: theme.onSurface,
              unselectedItemColor: theme.onSurface,
              selectedLabelStyle: TextStyle(
                color: theme.primary,
                fontSize: 13,
              ),
              unselectedLabelStyle: TextStyle(
                color: theme.onSurface.withOpacity(0.6),
                fontSize: 13,
              ),
              items: [
                for (var item in navItems)
                  BottomNavigationBarItem(
                    icon: Icon(
                      item.icon,
                      size: 22,
                    ),
                    label: item.label,
                  ),
              ],
            ),
          ),
        )
      ],
    );
  }
}
